package codeCreate.restfulAPICreate.util.TextAnalysis;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.lang.StringUtils;

import com.alibaba.fastjson.JSON;

import codeCreate.restfulAPICreate.controller.ApplyMessageCodeController;
import stream.StreamUtil;

public class ControllerAnalysis implements TextAnalysisInterface<Map<String, Map<String, String>>> {
    private String controllerTemplate = "/\\*\\*[\\s\\S]+?@Controller";
    private String controllerNoteTemplate = "\\* 类描述:[\\s\\S]+?\\* ";
    private String methodTemplate = "/\\*\\*[\\s\\S]+?@RequestMapping\\(value = ";
    private String controllerIntroduction = "controllerIntroduction";
    // private String methodNoteTemplate = "/\\*\\*[\\s\\S]+?@";
    private String methodNoteTemplate = "@Description[\\s\\S]+?@";
    private String methodIntroduction = "introduction";
    private String path;
    private Class<?> clazz;
    private String blank = "";
    private String[] methodFlag;

    public ControllerAnalysis(Class<?> clazz, String[] methodFlag) {
        this.methodFlag = methodFlag;
        this.clazz = clazz;
        this.path = new StringBuffer(System.getProperty("user.dir")).append("/src/")
                .append(clazz.getName().replace(".", "/")).append(".java").toString();
    }

    public ControllerAnalysis(Class<?> clazz, String controllerTemplate, String methodTemplate, String[] methodFlag,
            String methodNoteTemplate) {
        this.methodFlag = methodFlag;
        this.methodNoteTemplate = methodNoteTemplate;
        this.clazz = clazz;
        this.path = new StringBuffer(System.getProperty("user.dir")).append("/src/")
                .append(clazz.getName().replace(".", "/")).append(".java").toString();
        this.controllerTemplate = methodTemplate;
    }

    @Override
    public Map<String, Object> analyze() {
        String controllerFile = StreamUtil.outPutFile(path);
        String controllerInfo = find(controllerFile, controllerTemplate);
        controllerInfo = find(controllerInfo, controllerNoteTemplate);
        controllerInfo = dealControllerNote(controllerInfo);
        controllerFile = controllerFile.replace(controllerInfo, blank);
        Map<String, Object> methodsMap = null;
        for (String flag : methodFlag) {
            String currentTemplate = methodTemplate + "\"" + flag + "\"";
            String methodInfos = find(controllerFile, currentTemplate);
            if (StringUtils.isEmpty(methodInfos)) {
                continue;
            }
            // controllerFile = controllerFile.replace(methodInfos, "");
            if (methodsMap == null) {
                methodsMap = new HashMap<>();
            }
            Map<String, Object> methodMap = new HashMap<>();
            String note = find(methodInfos, methodNoteTemplate);
            note = dealMethodNote(note);
            methodMap.put(methodIntroduction, note);
            methodsMap.put(flag, methodMap);
        }
        if (methodsMap != null) {
            methodsMap.put(controllerIntroduction, controllerInfo);
        }

        System.out.println(JSON.toJSONString(methodsMap));
        return methodsMap;
    }

    private String find(String input, String template) {
        Matcher matcher = compilePattrn(template).matcher(input);
        String result = "";
        while (matcher.find()) {
            result = matcher.group(0);
        }
        return result;
    }

    private String replace(String input, String template, String replace) {
        Matcher matcher = compilePattrn(template).matcher(input);
        return matcher.replaceAll(replace);
    }

    private String dealMethodNote(String note) {
        return note.replace("@Description", "").replace("*", "").replace("@", "").trim();
    }

    private String dealControllerNote(String note) {
        return note.replace("* 类描述:", "").replace("*", "").trim();
    }

    private Pattern compilePattrn(String template) {
        Pattern pattern = Pattern.compile(template);
        return pattern;
    }

    public static void main(String[] args) throws IOException {
        String[] liStrings = { "/miguStaffApply", "cooperateStaffApply" };
        String controllerTemplate = "/\\*\\*[\\s\\S]+?@Controller";
        String methodTemplate = "/\\*\\*[\\s\\S]+?@RequestMapping";
        ControllerAnalysis controllerAnalysis = new ControllerAnalysis(ApplyMessageCodeController.class, liStrings);
        controllerAnalysis.analyze();
        // String string = "/**\rfsd\rfds\r*/";
        // Matcher matcher = Pattern.compile(template).matcher(string);
        // System.out.println(matcher.find());
    }

}
